package com.apicloud.nodulecreatenotification;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.uzmap.pkg.uzcore.uzmodule.UZModuleContext;

import org.json.JSONObject;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

public class CreateNotification {
    public static String EXTRA_PREFIX = "id";
    private static int autoIncNotificationId = 0; //自动递增的消息ID，用于多条消息管理
    private static NotificationManager notificationManager = null;
    public static Map<String, CreateNotification> list = new HashMap<>();//存储消息
    private static final String NOTIFICATION_CHANNEL_ID = "CreateNotification";//消息通道的ID
    public static String channelName = "通知消息";//消息通道的名称
    public static String channelDescription = "一般的通知消息";//消息通道的描述
    private static int icon;


    private Context context;

    public int id;
    public UZModuleContext moduleContext;
    public JSONObject data = null;
    public String title = null;
    public String content = null;
    public String img = null;
    public boolean autoCancel = true;
    public boolean ongoing = false;
    public int progress=-1;


    public CreateNotification(Context context, UZModuleContext moduleContext){
        this.context = context;
        this.moduleContext = moduleContext;
        if(notificationManager==null){
            notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            icon = context.getResources().getIdentifier("uz_icon","drawable", context.getPackageName());
            //开起消息通道
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                int importance = NotificationManager.IMPORTANCE_DEFAULT; //重要性[1-5]
                NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, importance);
                channel.setDescription(channelDescription);
                notificationManager.createNotificationChannel(channel);
            }
            EXTRA_PREFIX = context.getPackageName()+".";
        }
        id = ++autoIncNotificationId;
    }
    public int create(){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
        long ms = System.currentTimeMillis();
        //点击Intent
        Intent click = new Intent(context, ClickService.class);
        click.putExtra(EXTRA_PREFIX+"action", "click");
        click.putExtra(EXTRA_PREFIX+"noticeId", autoIncNotificationId+"");
        PendingIntent click_p = PendingIntent.getService(context, (int)ms, click, 0);

        Intent delete = new Intent(context, ClickService.class);
        delete.putExtra(EXTRA_PREFIX+"action", "delete");
        delete.putExtra(EXTRA_PREFIX+"noticeId", autoIncNotificationId+"");
        PendingIntent delete_p = PendingIntent.getService(context, (int)ms+1, delete, 0);

        builder.setContentTitle(title) //设置标题，必要
                .setContentText(content) //设置内容，必要
                .setSmallIcon(icon) //设置通知栏的小图标，必须设置
                .setWhen(ms) //设置时间，默认设置，可以忽略
                .setDefaults(Notification.FLAG_ONLY_ALERT_ONCE)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT) //设置优先级
                .setContentIntent(click_p) //设置在通知栏中点击该信息之后的操作
                .setDeleteIntent(delete_p) //设置在通知栏直接移除的操作
                .setOngoing(ongoing)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setOnlyAlertOnce(true)
                .setAutoCancel(autoCancel); //设置自动删除，点击通知栏信息之后系统会自动将状态栏的通知删除
        if(progress>=0 && progress<=1000){
            builder.setProgress(1000, progress, false); //设置大图标progress
        }
        if(img!=null){
            try {
                builder.setLargeIcon(fileBitmap(img)); //设置大图标
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        //存储数据，以便于在Service中提取
        list.put(id+"", this);
        notificationManager.notify(id, builder.build());
        return id;
    }

    /**
     * 本地图片的Bitmap
     * @param path
     * @return
     */
    private Bitmap fileBitmap(String path) throws FileNotFoundException {
        FileInputStream fis = new FileInputStream(path);
        return BitmapFactory.decodeStream(fis);
    }

    public void cancel(){
        //关闭通知
        notificationManager.cancel(id);
        //移除存储的JSONObject和UZModuleContext
        moduleContext.interrupt();
        CreateNotification.list.remove(id+"");
    }
}
